﻿using Polly;
using Polly.Timeout;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace PollyDemo
{
    /// <summary>
    /// polly 重试策略
    /// </summary>
    public static class PollyUtils
    {
        public static void Case1()
        {
            ISyncPolicy policy = Policy.Handle<ArgumentException>()
                .Fallback(() =>
                {
                    Console.WriteLine("Error occured");
                });

            policy.Execute(() =>
            {
                Console.WriteLine("Job Start");

                throw new ArgumentException("Hello Polly!");

                Console.WriteLine("Job End");
            });
        }

        public static void Case2()
        {
            ISyncPolicy policy = Policy.Handle<Exception>().Retry(3);

            try
            {
                policy.Execute(() =>
                {
                    Console.WriteLine("Job Start");
                    if (DateTime.Now.Second % 10 != 0)
                    {
                        throw new Exception("Special error occured");
                    }
                    Console.WriteLine("Job End");
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine("There's one unhandled exception : " + ex.Message);
            }
        }

        public static void Case3()
        {
            // Stop for 10s after retry 6 times
            ISyncPolicy policy = Policy.Handle<Exception>()
                .CircuitBreaker(6, TimeSpan.FromSeconds(10));

            while (true)
            {
                try
                {
                    policy.Execute(() =>
                    {
                        Console.WriteLine("Job Start");
                        throw new Exception("Special error occured");
                        Console.WriteLine("Job End");
                    });
                }
                catch (Exception ex)
                {
                    Console.WriteLine("There's one unhandled exception : " + ex.Message);
                }

                Thread.Sleep(500);
            }
        }

        public static void Case4()
        {
            try
            {
                ISyncPolicy policyException = Policy.Handle<TimeoutRejectedException>()
                    .Fallback(() =>
                    {
                        Console.WriteLine("Fallback");
                    });
                ISyncPolicy policyTimeout = Policy.Timeout(3, Polly.Timeout.TimeoutStrategy.Pessimistic);
                ISyncPolicy mainPolicy = Policy.Wrap(policyTimeout, policyException);
                mainPolicy.Execute(() =>
                {
                    Console.WriteLine("Job Start...");
                    Thread.Sleep(5000);
                    //throw new Exception();
                    Console.WriteLine("Job End...");
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Unhandled exception : {ex.GetType()} : {ex.Message}");
            }
        }

     
    }
}
